using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace model
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        //Model caja;
        Camara mainCamera;
        KeyboardState oldState;

        public List<Actor3D> actors = new List<Actor3D>();

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);

            graphics.PreferredBackBufferHeight = 600;
            graphics.PreferredBackBufferWidth = 800;

            mainCamera = new Camara();
            oldState = Keyboard.GetState();
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            actors.Add(new Actor3D(Content.Load<Model>("Iron Man"), new Vector3(0, -100, 200), new Vector3(1, 1, 1), 160.0f));
            actors.Add(new Actor3D(Content.Load<Model>("Iron Man"), new Vector3(300, -100, 200), new Vector3(1, 1, 1), 160.0f));
            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();
            UpdateInput();
            // TODO: Add your update logic here

            base.Update(gameTime);
        }
        
       /* Vector3 camPos = new Vector3(300, 200, 100);        
        Vector3 cajaPosition = new Vector3(0, -200, -450);
        float modelRot = 0.0f;
        float aspectRatio = 640.0f / 480.0f;
        */
        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            /*mainCamera = new Camara();
            Matrix[] transforms = new Matrix[caja.Bones.Count];//creo array de matrices (guarda matrices)
            caja.CopyAbsoluteBoneTransformsTo(transforms);*/
            GraphicsDevice.Clear(Color.CornflowerBlue);
            foreach (Actor3D act in actors)
            {
                Matrix[] transforms = new Matrix[act.MyModel.Bones.Count];
                act.MyModel.CopyAbsoluteBoneTransformsTo(transforms);

                foreach (ModelMesh mesh in act.MyModel.Meshes)
                {

                    foreach (BasicEffect effect in mesh.Effects)
                    {
                        effect.EnableDefaultLighting();
                        effect.World = transforms[mesh.ParentBone.Index] * Matrix.CreateRotationY(act.MyModelRot) * Matrix.CreateTranslation(act.position);
                        effect.View = Matrix.CreateLookAt(mainCamera.Position, mainCamera.Position+ mainCamera.Direction, Vector3.Up);
                        effect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f), mainCamera.aspectRatio, mainCamera.near, mainCamera.fieldOfView);
                    }
                    mesh.Draw();
                }                
            }
            

            // TODO: Add your drawing code here

            base.Draw(gameTime);
        }

        private void UpdateInput()
        {
            KeyboardState newState = Keyboard.GetState();

            // rots
            if (newState.IsKeyDown(Keys.Right))
            {
                mainCamera.rotate(new Vector3(0, -0.03f, 0));
            }
            if (newState.IsKeyDown(Keys.Left))
            {
                mainCamera.rotate(new Vector3(0, 0.03f, 0));
            }
            if (newState.IsKeyDown(Keys.Up))
            {
                mainCamera.rotate(new Vector3(-0.03f, 0, 0));
            }
            if (newState.IsKeyDown(Keys.Down))
            {
                mainCamera.rotate(new Vector3(0.03f, 0, 0));
            }
            // move
            if (newState.IsKeyDown(Keys.W))
            {
                mainCamera.Position += mainCamera.Direction * 1.5f;
            }
            if (newState.IsKeyDown(Keys.S))
            {
                mainCamera.Position -= mainCamera.Direction * 1.5f;
            }
            if (newState.IsKeyDown(Keys.A))
            {
                mainCamera.Position -= mainCamera.Left * 1.5f;
            }
            if (newState.IsKeyDown(Keys.D))
            {
                mainCamera.Position += mainCamera.Left * 1.5f;
            }
            // Otherwise, check to see whether it was down before.
            // (and therefore just released)
            else if (oldState.IsKeyDown(Keys.Space))
            {
                // Key has just been released.
            }

            oldState = newState;
        }
    }
}
